﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.卡尺;
using Halcon二次开发.View.多文本显示工具;
using Halcon二次开发.View.找圆_线_矩形;
using Halcon二次开发.View.文本显示工具;
using Halcon二次开发.View.距离转换工具;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.多文本显示
{
    public class ShowTextController
    {
        public HSmartWindowControl hw;

        //输入的区域图像
        public HObject HideIntputImage;
        //输入的卡尺行坐标 1 
        public double[] HideIntputArrayRows;
        //输入的卡尺列坐标 2  
        public double[] HideIntputArrayColumns;
        //输入的距离转换长度 3 
        public double[] HideIntputArrayDistances;
        //输入的结果分析 4
        public List<bool> HideIntputResults = new List<bool>();
        //窗体的对象
        public MessageBoxStringsForms form;
        public HwindowFlowNodeUrseControl hwindowFlowNode;
        public ShowTextController(MessageBoxStringsForms form, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //获取当前工位的HSmartWindowControl对象
            hw = hwindowFlowNode.hSmartWindowControl1;
            this.hwindowFlowNode = hwindowFlowNode;
            this.form = form;
        }

        public ShowTextController()
        {

        }

        /// <summary>
        /// 工具的单次运行
        /// </summary>
        public void Tool_Run()
        {
            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }
            hw.HalconWindow.ClearWindow();
            HOperatorSet.DispObj(HideIntputImage, hw.HalconWindow);
            Run();
        }


        /// <summary>
        /// 单次运行
        /// </summary>
        public void Run()
        {
            //显示在目标中心 靠右

            if (form.comboBox1.Text == "跟随")
            {
                if (HideIntputArrayRows == null || HideIntputArrayRows.Length == 0) return;
                for (int i = 0; i < HideIntputArrayRows.Length; i++)
                {
                    //设置文本显示的坐标
                    HOperatorSet.SetTposition(hw.HalconWindow, HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value);
                    //设置字体
                    HOperatorSet.SetFont(hw.HalconWindow, form.textBox3.Text + "-Normal-" + form.textBox4.Text.Trim());

                    if (HideIntputResults != null && HideIntputResults.Count != 0 && HideIntputResults[i])
                    {
                        //设置颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.comboBox2.Text);
                    }
                    else if (HideIntputResults != null && HideIntputResults.Count != 0 && !HideIntputResults[i])
                    {
                        //设置颜色
                        HOperatorSet.SetColor(hw.HalconWindow, form.comboBox3.Text);
                    }
                    else
                    {
                        //设置颜色
                        HOperatorSet.SetColor(hw.HalconWindow, "orange");
                    }
                    //设置文字
                    HOperatorSet.WriteString(hw.HalconWindow, form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"));
                }
            }

            //// 坐标或右边显示
            ////实时获取当前图片的大小
            //HOperatorSet.GetImageSize(HideIntputImage, out HTuple width, out HTuple height);
            ////记录一个变量，用于字体的往下走
            //int pix = 0;
            //if (MessageBoxStringsForms.comboBox1.Text.Equals("右边"))
            //{
            //    if (HideIntputDoubleTexts != null && HideIntputDoubleTexts.Length > 0)
            //    {
            //        for (int i = 0; i < HideIntputDoubleTexts.Length; i++)
            //        {
            //            HOperatorSet.SetTposition(hw.HalconWindow, (double)MessageBoxStringsForms.numericUpDown2.Value + pix, width + (double)MessageBoxStringsForms.numericUpDown1.Value - 200);
            //            HOperatorSet.SetFont(hw.HalconWindow, MessageBoxStringsForms.textBox3.Text + "-Normal-" + MessageBoxStringsForms.textBox4.Text.Trim());
            //            HOperatorSet.SetColor(hw.HalconWindow, MessageBoxStringsForms.textBox2.Text.Trim());
            //            HOperatorSet.WriteString(hw.HalconWindow, MessageBoxStringsForms.textBox1.Text + HideIntputDoubleTexts[i].ToString("F3"));
            //            pix += (int)MessageBoxStringsForms.numericUpDown3.Value;
            //        }
            //    }
            //}
            //else
            //{
            //    if (HideIntputDoubleTexts != null && HideIntputDoubleTexts.Length > 0)
            //    {
            //        for (int i = 0; i < HideIntputDoubleTexts.Length; i++)
            //        {
            //            HOperatorSet.SetTposition(hw.HalconWindow, (double)MessageBoxStringsForms.numericUpDown2.Value + pix, (double)MessageBoxStringsForms.numericUpDown1.Value);
            //            HOperatorSet.SetFont(hw.HalconWindow, MessageBoxStringsForms.textBox3.Text + "-Normal-" + MessageBoxStringsForms.textBox4.Text.Trim());
            //            HOperatorSet.SetColor(hw.HalconWindow, MessageBoxStringsForms.textBox2.Text.Trim());
            //            HOperatorSet.WriteString(hw.HalconWindow, MessageBoxStringsForms.textBox1.Text + HideIntputDoubleTexts[i].ToString("F3"));
            //            pix += (int)MessageBoxStringsForms.numericUpDown3.Value;
            //        }
            //    }
            //}


        }

        public void HardRun(MessageBoxStringsForms form,  double[] HideIntputArrayRows, double[] HideIntputArrayColumns, HSmartWindowControl hSmartWindowControl,    double[] HideIntputArrayDistances,List<bool> HideIntputResults)
        {
            if (form.comboBox1.Text == "跟随")
            {
                if (HideIntputArrayRows == null || HideIntputArrayRows.Length == 0) return;
                for (int i = 0; i < HideIntputArrayRows.Length; i++)
                {
                    if (HideIntputResults != null && HideIntputResults.Count != 0 && HideIntputResults[i])
                    {
                       

                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"), "image", HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value, form.comboBox2.Text, "box", 0);
                    }
                    else if (HideIntputResults != null && HideIntputResults.Count != 0 && !HideIntputResults[i])
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"), "image", HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value, form.comboBox3.Text, "box", 0);
                       
                    }
                    else
                    {
                        HOperatorSet.DispText(hSmartWindowControl.HalconWindow, form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"), "image", HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value, "orange", "box", 0);
                    }
                }
            }

        }
    }
}
